package com.cloud.wms.ui.returned.inquire;

import android.app.Activity;
import android.content.Intent;
import android.os.Message;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.cloud.wms.R;
import com.cloud.wms.base.BaseBarListActivity;
import com.cloud.wms.config.IntentKey;
import com.cloud.wms.ui.inside.entity.TransferCommodityEntity;
import com.cloud.wms.ui.returned.contact.InquireContact;
import com.cloud.wms.ui.returned.entity.InvoiceEntity;
import com.cloud.wms.ui.returned.presenter.InquirePresenter;

import java.util.ArrayList;

/**
 * Created by FC on 2021/5/21.
 * Desc：退货面单查询
 */
public class ReturnOrderInquireActivity extends BaseBarListActivity<InvoiceEntity, InquirePresenter> implements InquireContact.View {
    String returnOrderCode;

    @Override
    protected String barTitle() {
        return "退货面单查询";
    }

    @Override
    protected int getResId() {
        return R.layout.activity_ruturn_order_inquire;
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        ArrayList<InvoiceEntity> invoiceEntityList = getIntent().getParcelableArrayListExtra(IntentKey.LIST_KEY);
        returnOrderCode = getIntent().getStringExtra(IntentKey.ID_KEY);
        mAdapter.setNewInstance(invoiceEntityList);
    }

    @Override
    protected int getItemResId() {
        return R.layout.item_return_order;
    }

    @Override
    protected void bindItemData(BaseViewHolder helper, InvoiceEntity item) {
        helper.setText(R.id.tv_order_code, item.getInvoiceCode())
                .setText(R.id.tv_shipping_address, item.getShippingAddress())
                .setText(R.id.tv_delivery_time, item.getDeliveryTime());

        RecyclerView rvCommodity = helper.getView(R.id.rv_commodity);
        BaseQuickAdapter<TransferCommodityEntity, BaseViewHolder> commodityAdapter =
                new BaseQuickAdapter<TransferCommodityEntity, BaseViewHolder>(R.layout.item_face_sheet_start, item.getCommodityList()) {
                    @Override
                    protected void convert(@NonNull BaseViewHolder helper, TransferCommodityEntity commodityEntity) {
                        helper.setText(R.id.tv_bar_code, commodityEntity.getBarCode())
                                .setText(R.id.tv_commodity_name, commodityEntity.getCommodityName());
                    }
                };
        rvCommodity.setAdapter(commodityAdapter);
        helper.getView(R.id.btn_confirm).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPresenter.confirmToInventory(returnOrderCode, mAdapter.getItem(helper.getAdapterPosition()).getInvoiceID());
            }
        });
    }

    @Override
    public void onSuccess(Message message) {
        super.onSuccess(message);
        switch (message.what) {
            case InquirePresenter.CONFIRM_BIND_TO_INVENTORY:
                info("绑定成功，请进行清点");
                //todo 跳转清点页面
                break;
        }
    }

    public static void jumpToNext(Activity activity, String returnOrderCode, ArrayList<InvoiceEntity> invoiceEntityList) {
        Intent intent = new Intent(activity, ReturnOrderInquireActivity.class);
        intent.putParcelableArrayListExtra(IntentKey.LIST_KEY, invoiceEntityList);
        intent.putExtra(IntentKey.ID_KEY, returnOrderCode);
        activity.startActivity(intent);
    }

    @Override
    protected void setupComponent() {
        getActivityComponent().inject(this);
    }
}
